﻿#include "MeasureView/MeasureElement.h"
#include "MeasureView/MeasureDataSet.h"
namespace Qly {


//int MeasureElement::s_id = 0;

MeasureElement::MeasureElement(MeasureDataSet *pParent)
    : m_pParent(pParent),
      m_font(QFont(u8"Time New Roman", 14, QFont::Bold)),
      m_pen(QBrush(Qt::red), 3, Qt::SolidLine),
      m_measureType(None),
      m_measureState(DS_IDLE)

{
    int &id = m_pParent->element_id();
    ++id;
    m_id = id;
}

void MeasureElement::finish()
{
    m_measureState = DS_FINISH;
}

void MeasureElement::drawCross(QPainter &painter, QPointF p)
{
    painter.drawLine(p - QPointF(5, 0), p + QPointF(5, 0));
    painter.drawLine(p - QPointF(0, 5), p + QPointF(0, 5));
}

bool MeasureElement::isInTextBox(const QPointF &point)
{
    return m_textBlock.contains(point.toPoint());
}

MeasureElement::ElementType MeasureElement::type() const
{
    return m_measureType;
}

MeasureElement::MeasureState MeasureElement::state() const
{
    return m_measureState;
}

void MeasureElement::setLineWidth(int width)
{
    m_pen.setWidth(width);
}

void MeasureElement::setLineColor(QRgb rgb)
{
    m_pen.setColor(rgb);
}

const QVector<double> &MeasureElement::results() const
{
    return m_results_um;
}

const QVector<double> &MeasureElement::results_pix() const
{
    return m_results_pix;
}

const QVector<QPointF> &MeasureElement::keyPoints_pix() const
{
    return m_keyPoints_pix;
}

void MeasureElement::setTemplate(QString str)
{
    m_textTemplate = str;
}

int MeasureElement::setPixelSize(double newPixelSize_um, int method)
{
    if(m_keyPoints_pix.size() != m_keyPoints_um.size()) //这里表示出现了错误，也应该退出
    {
        qWarning() << "MeasureElement::setLens(). ERROR: m_keyPoints_pix.size() != m_keyPoints_um.size()";
        return 0;
    }
    if(newPixelSize_um == 0.0 )
    {
        qWarning() << "newPixelSize_um = 0";
        return 0;
    }
    if(method == 0) //只更新长度的数值，图元在图像上的位置不变
    {
        for(int  i = 0; i < m_keyPoints_pix.size(); ++i)
        {
            m_keyPoints_um[i] = m_keyPoints_pix[i] * newPixelSize_um;
        }
        compute();
        updateText();
    }
    else //method == 1 表示图元要相应的缩放。
    {

//        if(oldLens == newLens || oldLens == -1) return 0;// 这时不需要更新任何数据
//        LensData & oldData = lens.lensByIndex(oldLens);
//        LensData & newData = lens.currentLens();
//        double dx = (newData.xCompensate_um - oldData.xCompensate_um);
//        double dy = (newData.yCompensate_um - oldData.yCompensate_um);
        for(int  i = 0; i < m_keyPoints_pix.size(); ++i)
        {
            m_keyPoints_pix[i] = (m_keyPoints_um[i] /*+ QPointF(dx, dy)*/) / newPixelSize_um;
        }
        compute();
        updateText();
    }
    return 1;
}

//int MeasureElement::setLens(int oldLens, int newLens, int method)
//{
//    if(m_keyPoints_pix.size() != m_keyPoints_um.size()) //这里表示出现了错误，也应该退出
//    {
//        qDebug() << "MeasureElement::setLens(). ERROR: m_keyPoints_pix.size() != m_keyPoints_um.size()";
//        return 0;
//    }

//    LensManager & lens = LensManager::getInstance();
//    if(method == 0 && newLens != -1) //只更新长度的数值，图元在图像上的位置不变
//    {
//        double new_pixelSize = lens.currentLens().pixelSize_um;
//        for(int  i = 0; i < m_keyPoints_pix.size(); ++i)
//        {
//            m_keyPoints_um[i] = m_keyPoints_pix[i] * new_pixelSize;
//        }
//        compute();
//        updateText();
//    }
//    else //method == 1 表示图元要相应的缩放。
//    {
//        if(oldLens == newLens || oldLens == -1) return 0;// 这时不需要更新任何数据
//        LensData & oldData = lens.lensByIndex(oldLens);
//        LensData & newData = lens.currentLens();
//        double dx = (newData.xCompensate_um - oldData.xCompensate_um);
//        double dy = (newData.yCompensate_um - oldData.yCompensate_um);
//        for(int  i = 0; i < m_keyPoints_pix.size(); ++i)
//        {
//            m_keyPoints_pix[i] = (m_keyPoints_um[i] + QPointF(dx, dy)) / newData.pixelSize_um;
//        }
//        compute();
//        updateText();
//    }
//    return 1;
//}

void MeasureElement::updatePixelSize(double pixelsize_um)
{
    for(int i = 0; i < m_keyPoints_pix.size(); i++)
    {
        m_keyPoints_um[i] = m_keyPoints_pix[i] * pixelsize_um;
    }
}

QDebug operator<<(QDebug dbg, const MeasureElement::MeasureState &t)
{
    switch (t)
    {
    case MeasureElement::DS_IDLE:
        dbg << "MeasureElement::DS_IDLE";
        break;
    case MeasureElement::DS_ERROR:
        dbg << "MeasureElement::DS_ERROR";
        break;
    case MeasureElement::DS_FINISH:
        dbg << "MeasureElement::DS_FINISH";
        break;
    case MeasureElement::DS_STATE1:
        dbg << "MeasureElement::DS_STATE1";
        break;
    case MeasureElement::DS_STATE2:
        dbg << "MeasureElement::DS_STATE2";
        break;
    case MeasureElement::DS_STATE3:
        dbg << "MeasureElement::DS_STATE3";
        break;
    case MeasureElement::DS_STATE4:
        dbg << "MeasureElement::DS_STATE4";
        break;
    case MeasureElement::DS_STATE5:
        dbg << "MeasureElement::DS_STATE5";
        break;
    case MeasureElement::DS_STATE6:
        dbg << "MeasureElement::DS_STATE6";
        break;
    }
    return dbg;
}

QString MeasureElement::typeString() const
{
    switch (m_measureType)
    {
    case MeasureElement::None:
        return "None";
    case MeasureElement::Line:
        return "Line";
    case MeasureElement::Polyline:
        return "Polyline";
    case MeasureElement::Rect:
        return "Rect";
    case MeasureElement::Arc3P:
        return "Arc3P";
    case MeasureElement::Count:
        return "Count";
    case MeasureElement::Delete:
        return "Delete";
    case MeasureElement::Angle3P:
        return "Angle3P";
    case MeasureElement::Angle4P:
        return "Angle4P";
    case MeasureElement::Polygon:
        return "Polygon";
    case MeasureElement::Circle2P:
        return "Circle2P";
    case MeasureElement::Circle3P:
        return "Circle3P";
    case MeasureElement::CircleCenter:
        return "CircleCenter";
    case MeasureElement::Circle2Circle:
        return "Circle2Circle";
    case MeasureElement::ParallelLine:
        return "ParallelLine";
    case MeasureElement::Dots2Line:
        return "Dots2Line";
    }
    return QString();
}

QString type2String(const MeasureElement::ElementType &t)
{
    switch (t)
    {
    case MeasureElement::None:
        return "MeasureElement::None";
    case MeasureElement::Line:
        return "MeasureElement::Line";
    case MeasureElement::Polyline:
        return "MeasureElement::Polyline";
    case MeasureElement::Rect:
        return "MeasureElement::Rect";
    case MeasureElement::Arc3P:
        return "MeasureElement::Arc3P";
    case MeasureElement::Count:
        return "MeasureElement::Count";
    case MeasureElement::Delete:
        return "MeasureElement::Delete";
    case MeasureElement::Angle3P:
        return "MeasureElement::Angle3P";
    case MeasureElement::Angle4P:
        return "MeasureElement::Angle4P";
    case MeasureElement::Polygon:
        return "MeasureElement::Polygon";
    case MeasureElement::Circle2P:
        return "MeasureElement::Circle2P";
    case MeasureElement::Circle3P:
        return "MeasureElement::Circle3P";
    case MeasureElement::CircleCenter:
        return "MeasureElement::CircleCenter";
    case MeasureElement::Circle2Circle:
        return "MeasureElement::Circle2Circle";
    case MeasureElement::ParallelLine:
        return "MeasureElement::ParallelLine";
    case MeasureElement::Dots2Line:
        return "MeasureElement::Dots2Line";
    }
    return QString();
}

QDebug operator<<(QDebug dbg, const MeasureElement::ElementType &t)
{
    switch (t)
    {
    case MeasureElement::None:
        dbg << "MeasureElement::None";
        break;
    case MeasureElement::Line:
        dbg << "MeasureElement::Line";
        break;
    case MeasureElement::Polyline:
        dbg << "MeasureElement::Polyline";
        break;
    case MeasureElement::Rect:
        dbg << "MeasureElement::Rect";
        break;
    case MeasureElement::Arc3P:
        dbg << "MeasureElement::Arc3P";
        break;
    case MeasureElement::Count:
        dbg << "MeasureElement::Count";
        break;
    case MeasureElement::Delete:
        dbg << "MeasureElement::Delete";
        break;
    case MeasureElement::Angle3P:
        dbg << "MeasureElement::Angle3P";
        break;
    case MeasureElement::Angle4P:
        dbg << "MeasureElement::Angle4P";
        break;
    case MeasureElement::Polygon:
        dbg << "MeasureElement::Polygon";
        break;
    case MeasureElement::Circle2P:
        dbg << "MeasureElement::Circle2P";
        break;
    case MeasureElement::Circle3P:
        dbg << "MeasureElement::Circle3P";
        break;
    case MeasureElement::CircleCenter:
        dbg << "MeasureElement::CircleCenter";
        break;
    case MeasureElement::Circle2Circle:
        dbg << "MeasureElement::Circle2Circle";
        break;
    case MeasureElement::ParallelLine:
        dbg << "MeasureElement::ParallelLine";
        break;
    case MeasureElement::Dots2Line:
        dbg << "MeasureElement::Dots2Line";
        break;
    }
    return dbg;
}

void MeasureElement::textMove(const QPointF &offset_pix, const QPointF &offset_um)
{
    m_textMoving = true;
    m_textBlock.translate(offset_pix, offset_um);
}

MeasureElement::DeleteResult MeasureElement::tryDelete(QPointF p)
{
    if(isInTextBox(p))
    {
        return DeleteByOther;
    }
    return DeleteNothing;
}

void MeasureElement::setPen(const QPen &pen)
{
    m_pen = pen;
    m_textBlock.setPen(pen);
}

void MeasureElement::setFont(const QFont &font)
{
    m_font = font;
    m_textBlock.setFont(font);
}

int Qly::MeasureElement::mouseLButtonRelease(QPointF point_pix, QPointF point_um)
{
    Q_UNUSED(point_pix);
    Q_UNUSED(point_um);
    if(m_textMoving)
    {
        m_textMoving = false;
        return 1;
    }
    return 0;
}

int MeasureElement::mouseRButtonPress(QPointF point_pix, QPointF point_um)
{
    Q_UNUSED(point_pix);
    Q_UNUSED(point_um);
    return 0;
}

int MeasureElement::mouseMove(QPointF point_pix, QPointF point_um)
{
    Q_UNUSED(point_pix);
    Q_UNUSED(point_um);
    return 0;
}

}
